package com.cnap.model.file;

import cn.hutool.core.util.IdUtil;
import com.cnap.db.entity.PatchInfoPO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * 上传分片的请求
 *
 * @author xc
 **/
@ApiModel
public class UploadPatchReq {
    // 父id，应用id
    @ApiModelProperty(value = "父文件id")
    @NotBlank(message = "父文件id不能为空")
    private String parentId;
    // MD5
    @ApiModelProperty(value = "哈希值")
    private String md5;
    // 索引
    @ApiModelProperty(value = "索引")
    @DecimalMin(value = "0", message = "索引从0开始")
    private int index;
    // 文件名称
    @ApiModelProperty(value = "文件名")
    @NotBlank(message = "文件名称不能为空")
    @Pattern(regexp = "(.+)\\.(zip)$", message = "文件名非法，请使用zip压缩包")
    private String fileName;
    // 分片大小
    @ApiModelProperty(value = "分片大小")
    @DecimalMax(value = "9223372036854775807", message = "文件大小应小于9223372036854775807")
    private long patchSize;

    /**
     * id
     *
     * @return id
     */
    public String getParentId() {
        return parentId;
    }

    /**
     * set
     *
     * @param parentId id
     */
    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    /**
     * md5
     *
     * @return md5
     */
    public String getMd5() {
        return md5;
    }

    /**
     * set
     *
     * @param md5 md5
     */
    public void setMd5(String md5) {
        this.md5 = md5;
    }

    /**
     * index
     *
     * @return index
     */
    public int getIndex() {
        return index;
    }

    /**
     * set
     *
     * @param index index
     */
    public void setIndex(int index) {
        this.index = index;
    }

    /**
     * name
     *
     * @return name
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * set
     *
     * @param fileName name
     */

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    /**
     * size
     *
     * @return size
     */
    public long getPatchSize() {
        return patchSize;
    }

    /**
     * set
     *
     * @param patchSize size
     */
    public void setPatchSize(long patchSize) {
        this.patchSize = patchSize;
    }

    @Override
    public String toString() {
        return "parentId='" + parentId + '\'' +
                ", md5='" + md5 + '\'' +
                ", index=" + index +
                ", fileName='" + fileName + '\'' +
                ", patchSize='" + patchSize + '\'';
    }

    /**
     * 转换成po
     *
     * @return po
     */
    public PatchInfoPO toPO() {
        PatchInfoPO patchInfoPO = new PatchInfoPO();
        patchInfoPO.setId(IdUtil.simpleUUID());
        patchInfoPO.setParentId(this.parentId);
        patchInfoPO.setIndex(this.index);
        patchInfoPO.setFileName(this.fileName);
        patchInfoPO.setMd5("");
        patchInfoPO.setPatchSize(this.patchSize);
        patchInfoPO.setStatus(FileConstants.PATCH_STATUS_UPLOADING);

        return patchInfoPO;
    }

    /**
     * 生成响应
     *
     * @return 响应
     */
    public PatchRes toRes() {
        PatchRes patchRes = new PatchRes();
        patchRes.setParentId(this.parentId);
        patchRes.setIndex(this.index);
        patchRes.setMd5(this.md5);

        return patchRes;
    }
}
